package hk.linkcredit.walletservice.handler;

import hk.linkcredit.walletservice.constant.Status;
import hk.linkcredit.walletservice.exception.BizException;
import hk.linkcredit.walletservice.exception.JsonException;
import hk.linkcredit.walletservice.exception.PageException;
import hk.linkcredit.walletservice.exception.UtilsException;
import hk.linkcredit.walletservice.model.Result;
import hk.linkcredit.walletservice.utils.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 * 统一异常处理
 * </p>
 *
 * @author yangkai.shen
 * @date Created in 2018-10-02 21:26
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    private static final String DEFAULT_ERROR_VIEW = "error";

    /**
     * 统一 json 异常处理
     *
     * @param exception JsonException
     * @return 统一返回 json 格式
     */
    @ExceptionHandler(value = JsonException.class)
    @ResponseBody
    public Result jsonErrorHandler(JsonException exception) {
        log.error(">>>【JsonException】：{}", exception.getMessage());
        return Result.error(exception);
    }

    /**
     * 统一 Biz 异常处理
     *
     * @param exception BizException
     * @return 统一返回 json 格式
     */
    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public Result bizErrorHandler(BizException exception) {
        log.error(">>>【BizException】：{} - {}", exception.getCode(), exception.getMessage());
        return Result.error(exception);
    }

    /**
     * 统一 Utils 异常处理
     *
     * @param exception UtilsException
     * @return 统一返回 json 格式
     */
    @ExceptionHandler(value = UtilsException.class)
    @ResponseBody
    public Result utilsErrorHandler(UtilsException exception) {
        log.error(">>>【UtilsException】：{}", exception.getMessage());
        return Result.error(exception);
    }

    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    public Result handleException(HttpRequestMethodNotSupportedException e) {
        log.error(">>>【HttpRequestMethodNotSupportedException】：{}", ServletUtils.getRequest().getRequestURL() + " - " + e.getMessage(), e);
        return Result.failure(Status.SYSTEM_BUSY);
    }

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        log.error(">>>【Exception】：{}", e.getMessage(), e);
        return Result.failure(Status.SYSTEM_BUSY);
    }

    /**
     * 统一 页面 异常处理
     *
     * @param exception PageException
     * @return 统一跳转到异常页面
     */
    @ExceptionHandler(value = PageException.class)
    public ModelAndView pageErrorHandler(PageException exception) {
        log.error("【DemoPageException】：{}", exception.getMessage());
        ModelAndView view = new ModelAndView();
        view.addObject("message", exception.getMessage());
        view.setViewName(DEFAULT_ERROR_VIEW);
        return view;
    }
}
